package cn.itcast.dewu.controller;

import cn.hutool.core.io.FileUtil;
import cn.itcast.dewu.config.CloudStorageConfig;
import cn.itcast.dewu.dto.Result;
import cn.itcast.dewu.utils.OSSFactory;
import cn.itcast.dewu.utils.QiniuCloudStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Path;

@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {
    @PostMapping("/blog")
    public Result uploadImage(MultipartFile file) {
        if (file.isEmpty()) {
            return Result.fail("上床文件不能为空^_^");
        }
        String fileName = file.getName();
        //上传文件
        CloudStorageConfig config = OSSFactory.getConfigObject(CloudStorageConfig.class);
        QiniuCloudStorageService qiniuCloudStorageService = new QiniuCloudStorageService(config);
        String path = null;
        try {
             path = qiniuCloudStorageService.upload(file,"blog");
            System.err.println(path);
        } catch (Exception e) {
            log.error("文件上传失败，{}", fileName);
            throw new RuntimeException("上传文件失败",e);
        }

        log.debug("文件上传成功，{}", fileName);
        return Result.ok(path);
    }

}
